﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using MySpace.Data;
using DataBaseTable;
public partial class Manage_New_mode : System.Web.UI.Page
{
    private string type = "";//添加信息类型
    private string newid = "";
    BindMethod MyMethod;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.type = string.IsNullOrEmpty(Request.QueryString["type"]) ? "" : Request.QueryString["type"];
        this.newid = Request.QueryString["newid"];
        if (this.type.Equals("dengxin"))
        {
            this.MyMethod = new DengXinMethod(this,this.newid);
            this.HDTime_panel.Visible = false;
        }
        else
        {
            this.MyMethod = new NewsMethod(this,this.newid);
        }
        if (!IsPostBack)
        {
            //contents.FontFacesMenuList = new string[] { "宋体", "新宋体", "黑体", "幼圆", "Arial" };
            BindSelect();
            bd();
        }
    }
    protected void bd()
    {
        this.MyMethod.InitData();
    }
    protected void BindSelect()
    {
        this.MyMethod.BindBigSelect();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(title.Text.Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('请输入标题')", true);
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请输入标题');</script>");
            return;
        }
        if (!testbigclass())
        {
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('请选择大类')", true);
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请选择大类');</script>");
            return;
        }
        if (!testsmallclass())
        {
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('请选择小类')", true);
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请选择小类');</script>");
            return;
        }
        this.MyMethod.UpdateArticle();

    }
    protected void bigclassid_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!testbigclass())
        {
            testbig.Text = "*";
            testsmall.Text = "*";
        }
        this.MyMethod.BindSmallSelect();
        
    }
    protected bool testbigclass()
    {
        if (bigclassid.SelectedItem.Value == "0")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    protected bool testsmallclass()
    {
        if (smallclassid.SelectedItem.Value == "0")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public interface BindMethod
    {
        //方法接口
        void BindBigSelect();
        void BindSmallSelect();
        void InitData();
        void UpdateArticle();
    }
    public class NewsMethod : BindMethod
    {
        DropDownList bigclassid;
        DropDownList smallclassid;
        TextBox title;
        CheckBox shenhe;
        CheckBox istop;
        TextBox author;
        TextBox HDTime_tb;
        HiddenField contents;
        HiddenField contents_text;
        UpdatePanel UpdatePanel1;
        Label nowbig;
        Label nowsmall;
        HiddenField Hiddenid;
        string newid;
        public NewsMethod(Manage_New_mode page, string newid)
        {
            this.bigclassid = page.bigclassid;
            this.smallclassid = page.smallclassid;
            this.title = page.title;
            this.shenhe = page.shenhe;
            this.istop = page.istop;
            this.author = page.author;
            this.HDTime_tb = page.HDTime_tb;
            this.contents = page.contents;
            this.contents_text = page.contents_text;
            this.UpdatePanel1 = page.UpdatePanel1;
            this.nowbig = page.nowbig;
            this.nowsmall = page.nowsmall;
            this.Hiddenid = page.Hiddenid;
            this.newid = newid;
        }
        public void BindBigSelect()
        {
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = "select bigclassid,bigclassname from News_big_type";
            DataTable dtb = dbh.ExecuteDataTable();
            DataRow drb = dtb.NewRow();
            drb[0] = 0;
            drb[1] = "--请选择大类--";
            dtb.Rows.InsertAt(drb, 0);
            bigclassid.DataSource = dtb;
            bigclassid.DataTextField = "bigclassname";
            bigclassid.DataValueField = "bigclassid";
            bigclassid.DataBind();
        }
        public void BindSmallSelect()
        {
            smallclassid.Visible = true;
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = string.Format("select smallclassid,smallclassname from News_small_type where forbigclass={0}", Convert.ToInt32(bigclassid.SelectedItem.Value));
            DataTable dt = dbh.ExecuteDataTable();
            DataRow dr = dt.NewRow();
            dr[0] = 0;
            dr[1] = "--请选择小类--";
            dt.Rows.InsertAt(dr, 0);
            smallclassid.DataSource = dt;
            smallclassid.DataTextField = "smallclassname";
            smallclassid.DataValueField = "smallclassid";
            smallclassid.DataBind();
        }
        public void InitData()
        {
            if (!string.IsNullOrEmpty(this.newid))
            {
                NewsTable nt = new NewsTable(Convert.ToInt32(this.newid));
                title.Text = nt.NewTitle;
                if (nt.Shenhe == 1)
                {
                    shenhe.Checked = true;
                }
                else
                {
                    shenhe.Checked = false;
                }
                if (nt.IsTop == 1)
                {
                    istop.Checked = true;
                }
                else
                {
                    istop.Checked = false;
                }
                DBHandler dbh = GetHandler.Init();
                dbh.CommandText = string.Format("select smallclassid,smallclassname from News_small_type where forbigclass={0}", Convert.ToInt32(nt.BigClassId));
                DataTable dts = dbh.ExecuteDataTable();
                DataRow drs = dts.NewRow();
                drs[0] = 0;
                drs[1] = "--请选择小类--";
                dts.Rows.InsertAt(drs, 0);
                smallclassid.DataSource = dts;
                smallclassid.DataTextField = "smallclassname";
                smallclassid.DataValueField = "smallclassid";
                smallclassid.DataBind();
                SmallClassTable classs = new SmallClassTable(nt.SmallClassId);
                nowbig.Text = classs.ForBigClassName;
                nowsmall.Text = classs.SmallClassName;
                author.Text = nt.Author;
                HDTime_tb.Text = nt.HDTime;
                contents.Value = nt.Contents;
                Hiddenid.Value = nt.ID.ToString();
                bigclassid.SelectedValue = nt.BigClassId.ToString();
                smallclassid.SelectedValue = nt.SmallClassId.ToString();


            }
        }
        public void UpdateArticle()
        {
            NewsTable ntb = new NewsTable(Convert.ToInt32(Hiddenid.Value));
            ntb.NewTitle = title.Text.ToString();
            ntb.BigClassId = Convert.ToInt32(bigclassid.SelectedItem.Value);
            if (!string.IsNullOrEmpty(smallclassid.SelectedItem.Value))
            {
                ntb.SmallClassId = Convert.ToInt32(smallclassid.SelectedItem.Value);

            }
            if (shenhe.Checked == true)
            {
                ntb.Shenhe = 1;
            }
            else
            {
                ntb.Shenhe = 0;
            }
            if (istop.Checked == true)
            {
                ntb.IsTop = 1;
            }
            else
            {
                ntb.IsTop = 0;
            }
            ntb.Author = author.Text.ToString();
            ntb.HDTime = HDTime_tb.Text.ToString();
            ntb.Contents = contents.Value.ToString();
            ntb.texts = contents_text.Value.ToString();
            try
            {
                ntb.Update();
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('修改文章成功');window.location.href='News_List.aspx';", true);
                //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加文章成功');window.location.href='News_List.aspx';</script>");
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('修改文章失败')", true);
                //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('修改文章失败');</script>");
            }
        }
    }


    public class DengXinMethod : BindMethod
    {
        DropDownList bigclassid;
        DropDownList smallclassid;
        TextBox title;
        CheckBox shenhe;
        CheckBox istop;
        TextBox author;
        HiddenField contents;
        UpdatePanel UpdatePanel1;
        Label nowbig;
        Label nowsmall;
        HiddenField Hiddenid;
        string newid;
        public DengXinMethod(Manage_New_mode page, string newid)
        {
            this.bigclassid = page.bigclassid;
            this.smallclassid = page.smallclassid;
            this.title = page.title;
            this.shenhe = page.shenhe;
            this.istop = page.istop;
            this.author = page.author;
            this.contents = page.contents;
            this.UpdatePanel1 = page.UpdatePanel1;
            this.nowbig = page.nowbig;
            this.nowsmall = page.nowsmall;
            this.Hiddenid = page.Hiddenid;
            this.newid = newid;
        }
        public void BindBigSelect()
        {
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = "select BookId,PeriodsName from DengXinBook order by DIndex desc";
            DataTable dtb = dbh.ExecuteDataTable();
            DataRow drb = dtb.NewRow();
            drb[0] = 0;
            drb[1] = "--请选择期数--";
            dtb.Rows.InsertAt(drb, 0);
            bigclassid.DataSource = dtb;
            bigclassid.DataTextField = "PeriodsName";
            bigclassid.DataValueField = "BookId";
            bigclassid.DataBind();
        }
        public void BindSmallSelect()
        {
            smallclassid.Visible = true;
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = string.Format("select CatalogueId,ChapterName from DengXinCatalogue where ForPeriodsId={0}", Convert.ToInt32(bigclassid.SelectedItem.Value));
            DataTable dt = dbh.ExecuteDataTable();
            DataRow dr = dt.NewRow();
            dr[0] = 0;
            dr[1] = "--请选择章节--";
            dt.Rows.InsertAt(dr, 0);
            smallclassid.DataSource = dt;
            smallclassid.DataTextField = "ChapterName";
            smallclassid.DataValueField = "CatalogueId";
            smallclassid.DataBind();
        }
        public void InitData()
        {
            if (!string.IsNullOrEmpty(this.newid))
            {
                DengXinTable cxt = new DengXinTable(Convert.ToInt32(this.newid));
                title.Text = cxt.NewTitle;
                if (cxt.Shenhe == 1)
                {
                    shenhe.Checked = true;
                }
                else
                {
                    shenhe.Checked = false;
                }
                if (cxt.IsTop == 1)
                {
                    istop.Checked = true;
                }
                else
                {
                    istop.Checked = false;
                }
                DBHandler dbh = GetHandler.Init();
                dbh.CommandText = string.Format("select CatalogueId,ChapterName from DengXinCatalogue where ForPeriodsId={0}", Convert.ToInt32(cxt.PeriodsId));
                DataTable dts = dbh.ExecuteDataTable();
                DataRow drs = dts.NewRow();
                drs[0] = 0;
                drs[1] = "--请选择章节--";
                dts.Rows.InsertAt(drs, 0);
                smallclassid.DataSource = dts;
                smallclassid.DataTextField = "ChapterName";
                smallclassid.DataValueField = "CatalogueId";
                smallclassid.DataBind();
                DengXinCatalogueTable classs = new DengXinCatalogueTable(cxt.ChapterId);
                nowbig.Text = classs.ForPeriodsName;
                nowsmall.Text = classs.ChapterName;
                author.Text = cxt.Author;
                contents.Value = cxt.Contents;
                Hiddenid.Value = cxt.ID.ToString();
                bigclassid.SelectedValue = cxt.PeriodsId.ToString();
                smallclassid.SelectedValue = cxt.ChapterId.ToString();


            }
        }
        public void UpdateArticle()
        {
            DengXinTable dxt = new DengXinTable(Convert.ToInt32(Hiddenid.Value));
            dxt.NewTitle = title.Text.ToString();
            dxt.PeriodsId = Convert.ToInt32(bigclassid.SelectedItem.Value);
            if (!string.IsNullOrEmpty(smallclassid.SelectedItem.Value))
            {
                dxt.ChapterId = Convert.ToInt32(smallclassid.SelectedItem.Value);

            }
            if (shenhe.Checked == true)
            {
                dxt.Shenhe = 1;
            }
            else
            {
                dxt.Shenhe = 0;
            }
            if (istop.Checked == true)
            {
                dxt.IsTop = 1;
            }
            else
            {
                dxt.IsTop = 0;
            }
            dxt.Author = author.Text.ToString();
            dxt.Contents = contents.Value.ToString();
            try
            {
                dxt.Update();
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('修改文章成功');window.location.href='News_List.aspx';", true);
                //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加文章成功');window.location.href='News_List.aspx';</script>");
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('修改文章失败')", true);
                //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('修改文章失败');</script>");
            }
        }

    }
}